package com.newgrand.utils;

import com.newgrand.domain.dbo.SecDevDBLog;
import com.newgrand.mapper.SecDevDBLogMapper;
import com.newgrand.utils.uuid.NewPhidUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;

/**
 * @创建人：ZhaoFengjie
 * @修改人：ZhaoFengjie
 * @创建时间：17:15 2022/10/13
 * @修改时间:：17:15 2022/10/13
 */

@Slf4j
@Component
public class NGLog {

    @Resource
    private SecDevDBLogMapper logMapper;

    //    private final String oriphid;
//    private final String tabname;
//    private final String biz;
    private static NGLog ngLog;

    @PostConstruct
    public void init() {
        ngLog = this;
        ngLog.logMapper = this.logMapper;
    }

    @SneakyThrows
    public NGLog() {

    }


    @Async(value = "asyncServiceExecutor")
    public void info(String biz, String msg) {
        try {
            InsertLog("info", biz, msg, "", "", "");
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }

    @Async(value = "asyncServiceExecutor")
    public void info(String biz, String msg, String oriphid, String tabname) {
        try {
            InsertLog("info", biz, msg, "", oriphid, tabname);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }

    @Async(value = "asyncServiceExecutor")
    public void error(String biz, String msg, String errmsg) {
        try {
            InsertLog("error", biz, msg, errmsg, "", "");
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }

    @Async(value = "asyncServiceExecutor")
    public void error(String biz, String msg, String errmsg, String oriphid, String tabname) {
        try {
            InsertLog("error", biz, msg, errmsg, oriphid, tabname);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }

    private static void InsertLog(String level, String biz, String message, String errmsg, String oriphid, String tabname) {
        String phid = NewPhidUtils.getPhid();
        SecDevDBLog secDevDBLog = SecDevDBLog.builder()
                .phid(phid)
                .lev(level)
                .biz(biz)
                .msg(message)
                .errmsg(errmsg)
                .ct(new Date())
                .oriphid(oriphid)
                .tabname(tabname)
                .build();
        ngLog.logMapper.insert(secDevDBLog);
    }
}
